package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.db.UserAddressDO;
import com.wisewin.circle.service.UserAddressService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/address")
public class UserAddressController extends BaseController {

    @Autowired
    private UserAddressService userAddressService;

    // 添加收货地址
    @PostMapping("/addUserAddress")
    public ResultDTO addUserAddress (HttpServletRequest request, @RequestBody UserAddressDO userAddressDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        userAddressDO.setUserId(loginUser.getId());
        return userAddressService.addUserAddress(userAddressDO);
    }

    // 修改收货地址
    @PostMapping("/updateUserAddress")
    public ResultDTO updateUserAddress (HttpServletRequest request, @RequestBody UserAddressDO userAddressDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        userAddressDO.setUserId(loginUser.getId());
        return userAddressService.updateUserAddress(userAddressDO);
    }

    // 查询收货地址详细信息
    @PostMapping("/queryUserAddressDetail")
    public ResultDTO queryUserAddressDetail (HttpServletRequest request, @RequestBody UserAddressDO userAddressDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        userAddressDO.setUserId(loginUser.getId());
        return userAddressService.queryUserAddressDetail(userAddressDO);
    }

    // 查询用户地址
    @PostMapping("/queryUserAddress")
    public ResultDTO queryUserAddress (HttpServletRequest request) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return userAddressService.queryUserAddress(loginUser.getId());
    }
}
